﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyDMS.DAL;

public partial class ProjectPage : PageBaseClass
{
    MyDMSEntity _myDmsEntity = new MyDMSEntity();
    public string ProjectName = string.Empty;
    private int _proj_code = 0;
    private bool _isAnony = false;
    protected int _dept_code = 0;
    protected int _assgin_code = 0;
    protected void Page_Load(object sender, EventArgs e)
    {

        #region 处理参数
        if (Request.QueryString["proj"] != null)
        {
            _proj_code = int.Parse(Request.QueryString["proj"]);
            var firstOrDefault = _myDmsEntity.Project.FirstOrDefault(p => p.Project_Code == _proj_code);
            if (firstOrDefault != null)
                _assgin_code = firstOrDefault.Assigned_To;
        }
        if (Request.QueryString["dept"] != null)
        {
            _dept_code = int.Parse(Request.QueryString["dept"]);
        }

        #endregion

        _isAnony = _myDmsEntity.Project.FirstOrDefault(p => p.Project_Code == _proj_code).Is_Anonymous;
        if (Author.department_Code == _dept_code || _isAnony)
        {
            #region 数据绑定


            var documentDatasource =
                from task in _myDmsEntity.Task.Where(t => t.Project_Code == _proj_code)
                select new
                           {
                               task.Task_Code,
                               task.Task_Title,
                               task.Task_Desc,
                               AssginName =
                    _myDmsEntity.User.FirstOrDefault(uu => uu.User_Code == task.Assigned_To).FirstName +
                    _myDmsEntity.User.FirstOrDefault(uu => uu.User_Code == task.Assigned_To).LastName,
                               Document_Count = _myDmsEntity.Document.Count(d => d.Task_Code == task.Task_Code),
                               Folder_Count = _myDmsEntity.Folder.Count(f => f.Task_Code == task.Task_Code),
                               task.Creation_Date,
                               task.Updation_Date
                           };
            gvTasksManaged.DataSource = documentDatasource;
            gvTasksManaged.DataBind();

            if (!IsPostBack)
            {
                ddlUsers.DataSource = from u in _myDmsEntity.User
                                      where u.Department_Code == Author.department_Code
                                      select new {FullName = u.FirstName + u.LastName, u.User_Code};

                ddlUsers.DataTextField = "FullName";
                ddlUsers.DataValueField = "User_Code";
                ddlUsers.DataBind();
            }

            #endregion
        }
    }
    protected void gvTasksManaged_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "OpenChildren")
        {
            int taskCode = Convert.ToInt32(e.CommandArgument);
            Response.Redirect(Request.Url.AbsoluteUri.ToLower().Replace("project.aspx","Task.aspx")+"&task="+taskCode);
        }

    }
    protected void btnAddTask_Click(object sender, EventArgs e)
    {
        Task ptask = new Task();
        ptask.Task_Title = txtTaskTitle.Text;
        ptask.Task_Desc = txtTastDesc.Text;
        ptask.Assigned_To = int.Parse(ddlUsers.SelectedValue);
        ptask.Creation_By = Author.user_Code;
        ptask.Creation_Date = DateTime.Parse(txtTaskOpenDate.Text);
        ptask.Project_Code = _proj_code;

        _myDmsEntity.Task.AddObject(ptask);

        if (_myDmsEntity.SaveChanges() > 0)
        {
            Response.Redirect("javascript:$('#dialogAddChildren').dialog('close');alert('添加成功，由"+ddlUsers.SelectedItem.Text+"负责');location.reload(true);");
        }
        else
        {
            Response.Redirect("javascript:$('#dialogAddChildren').dialog('close');alert('添加失败');location.reload(true);");
        }
    }
}